﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MediatorPattern.DemoTwo
{
    public class Mediator : AbstractMediator
    {
        /// <summary>
        /// 中介者最重要的方法
        /// </summary>
        /// <param name="str"></param>
        /// <param name="objects"></param>
        public override void execute(string str,params object[] objects)
        {
            if (str.Equals("purchase.buy"))
            {
                buyComputer((int)objects[0]);
            }
            else if (str.Equals("sale.sell"))
            {
                sellComputer((int)objects[0]);
            }
            else if (str.Equals("sale.offsell"))
            {
                offSell();
            }
            else if (str.Equals("stock.clear"))
            {
                clearStock();
            }
        }

        /// <summary>
        /// 採購電腦
        /// </summary>
        /// <param name="number"></param>
        private void buyComputer(int number)
        {
            int saleStatus = sale.getSaleStatus();

            if (saleStatus > 80)
            {
                stock.increase(number);
            }
            else
            {
                int buyNumber = number / 2;
            }
        }

        /// <summary>
        /// 銷售電腦
        /// </summary>
        /// <param name="number"></param>
        private void sellComputer(int number)
        {
            if (stock.getStockNumber() < number)
            {
                purchase.buyIBMComputer(number);
            }

            stock.decrease(number);
        }

        /// <summary>
        /// 折扣銷售電腦
        /// </summary>
        private void offSell()
        {

        }

        /// <summary>
        /// 清倉處理
        /// </summary>
        private void clearStock()
        {
            sale.offSale();

            purchase.refuseBuyIBM();
        }

    }
}
